<template>
	<view class="normal-login-container">

		<view class="logo-content">
			<view class="title">花季护航</view>
			<view class="pinyin">
				hua&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ji&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hu&nbsp;&nbsp;
				hang</view>
		</view>

		<view class="login-form-content">
			<view class="input-item flex align-center">
				<view class="iconfont" style="font-size: 60rpx;margin-left: 10rpx;">&#xe60d;</view>
				<input v-model="loginForm.username" class="input" type="text" placeholder="请输入账号" maxlength="30" />
			</view>
			<view class="input-item flex align-center">
				<view class="iconfont" style="font-size: 60rpx;margin-left: 10rpx;">&#xe65c;</view>
				<input v-model="loginForm.password" type="password" class="input" placeholder="请输入密码" maxlength="20" />
			</view>
			<view class="input-item flex align-center" v-if="captchaEnabled">
				<view class="iconfont" style="font-size: 60rpx;margin-left: 10rpx;">&#xe708;</view>
				<input v-model="loginForm.code" type="number" class="input" placeholder="请输入验证码" maxlength="4" />
				<image :src="codeUrl" @click="getCode" class="login-code-img"></image>
			</view>
			<div class="AgreementRadioBox">
				<view class="protocol reg1">
					<span id="isSelProto" class="red-checkbox"
						:class="{'red-checkbox':loginForm.checked==false,'red-checkbox active':loginForm.checked==true}"
						@click="xy()"></span>已阅并同意
					<text class="text-black">
						登录即代表您同意
					</text>
					<text @click="handleUserAgrement" class="text-yellow text">《花季护航用户使用协议》</text>和
					<text @click="handlePrivacy" class="text-yellow text">《花季护航隐私协议》</text>
				</view>
			</div>


			<view class="button-group">
				<button @click="handleLogin" class="btn">登录</button>
			</view>
			<view class="footer">
				<text class="left" @click="register">
					注册新用户
				</text>
				|
				<text class="right" @click="pwd">
					忘记密码
				</text>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		getCodeImg
	} from '@/api/login'

	export default {
		data() {
			return {
				codeUrl: "",
				captchaEnabled: true,
				globalConfig: getApp().globalData.config,
				loginForm: {
					username: "",
					password: "",
					code: "",
					uuid: '',
					checked: false
				},

			}
		},
		created() {
			let that = this
			this.getCode()
		},
		methods: {
			xy() //点击后
			{
				// var nav = document.getElementById("isSelProto");
				// nav.className = isSelProto.className == "red-checkbox" ? "red-checkbox active" : "red-checkbox";
				if (this.loginForm.checked) {
					this.loginForm.checked = false
				} else {
					this.loginForm.checked = true
				}
			},

			register() {
				this.$tab.navigateTo(`/pages/register`)
			},
			pwd() {
				this.$tab.navigateTo(`/pages/pwd`)
			},
			// 隐私协议
			handlePrivacy() {
				// 	let site = this.globalConfig.appInfo.agreements[0]
				// 	this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
			},
			// 用户协议
			handleUserAgrement() {
				// let site = this.globalConfig.appInfo.agreements[1]
				// this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
			},
			// 获取图形验证码
			getCode() {
				getCodeImg().then(res => {
					this.captchaEnabled = res.data.captchaEnabled === undefined ? true : res.data
						.captchaEnabled
					if (this.captchaEnabled) {
						this.codeUrl = 'data:image/gif;base64,' + res.data.img
						this.loginForm.uuid = res.data.uuid
					}
				})
			},
			// 登录方法
			async handleLogin() {
				if (this.loginForm.checked === false) {
					this.$modal.msgError("请阅读并同意用户协议及隐私政策")
				} else
				if (this.loginForm.username === "") {
					this.$modal.msgError("请输入您的账号")
				} else if (this.loginForm.password === "") {
					this.$modal.msgError("请输入您的密码")
				} else if (this.loginForm.code === "" && this.captchaEnabled) {
					this.$modal.msgError("请输入验证码")
				} else {
					this.$modal.loading("登录中，请耐心等待...")
					this.pwdLogin()
				}
			},
			// 密码登录
			async pwdLogin() {
				this.$store.dispatch('Login', this.loginForm).then(() => {
					this.$modal.closeLoading()
					this.loginSuccess()
				}).catch(() => {
					if (this.captchaEnabled) {
						this.getCode()
					}
				})
			},
			// 登录成功后，处理函数
			loginSuccess(result) {
				// 设置用户信息
				this.$store.dispatch('GetInfo').then(res => {
					this.$tab.reLaunch('/pages/home/index')
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		width: 100%;
		height: 100%;
		background-color: #ffffff;
		background: url(../static/login/login.png) no-repeat;
		background-size: 100% 100%;
	}



	.normal-login-container {

		.logo-content {
			font-size: 60rpx;
			text-align: center;
			padding-top: 40%;
			font-weight: bold;


			.title {
				color: #f0bf19;

			}

			.pinyin {
				color: #f0bf19;
				width: 400rpx;
				font-size: 20rpx;
				margin: auto;
				font-weight: normal;
				padding-bottom: 20rpx;
				border-bottom: 2rpx solid #f0f0f0;
			}
		}

		.login-form-content {
			text-align: center;
			margin: 20px auto;
			margin-top: 5%;
			width: 80%;

			.input-item {
				width: 550rpx;
				margin: 40rpx auto;
				height: 90rpx;
				border-bottom: 2rpx solid #f0f0f0;

				.input {
					width: 100%;
					font-size: 30rpx;
					line-height: 45rpx;
					text-align: left;
					padding-left: 10rpx;
				}

			}


			.button-group {
				margin-top: 30rpx;
				display: flex;
				justify-content: space-around;
			}

			.btn {
				width: 550rpx;
				border-radius: 0rpx !important;
				height: 70rpx;
				background-color: #f7cb50;
				color: #ffffff;
				font-size: 24rpx;
				line-height: 60rpx;
			}

			.footer {

				width: 500rpx;
				margin: auto;
				margin-top: 30rpx;
				font-size: 27rpx;
				color: #9e9e9e;
				display: flex;
				justify-content: space-around;
			}

			.AgreementRadioBox {
				margin-left: 25rpx;
				font-size: 25rpx;

				.text {
					text-decoration: underline;
				}
			}


		}

	}

	.login-code-img {
		height: 90rpx;
	}

	.red-checkbox {
		background-image: url('https://img.58cdn.com.cn/git/teg-app-fe/passport-pc-ui/img/user-list-uncheck.png'); //为了方便易懂，这边图放的是58的链接，仅供学习参考。
		height: 17px;
		width: 17px;
		background-size: 14px 14px;
		background-repeat: no-repeat;
		vertical-align: middle;
		display: inline-block;
		cursor: pointer;
	}

	.active {
		background-image: url('https://img.58cdn.com.cn/git/teg-app-fe/passport-pc-ui/img/user-list-check.png'); //为了方便易懂，这边图放的是58的链接，仅供学习参考。
	}

	.error_msg {
		display: none;
		color: red;
	}
</style>
